package com.ysu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysu.demo.dataobject.Income;
import com.ysu.demo.dto.IncomeDTOGoodPeople;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.enums.IncomeState;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.income.IncomeCreateFrom;
import com.ysu.demo.mapper.IncomeMapper;
import com.ysu.demo.service.IGoodPeopleService;
import com.ysu.demo.service.IIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysu.demo.service.IProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@Service
public class IncomeServiceImpl extends ServiceImpl<IncomeMapper, Income> implements IIncomeService {
    @Autowired
    IGoodPeopleService goodPeopleService;

    @Autowired
    IProjectService projectService;

    @Override
    public List<IncomeDTOGoodPeople> findDTO(Integer projectId) {
        List<Income> incomes = this.list(new QueryWrapper<Income>()
                .eq("project_id",projectId));

        List<IncomeDTOGoodPeople> res = new ArrayList<>();

        for(Income income: incomes){
            IncomeDTOGoodPeople one = new IncomeDTOGoodPeople();

            BeanUtils.copyProperties(income,one);

            one.setGoodPeople(goodPeopleService.getById(income.getGoodPeopleId()));

            res.add(one);
        }

        return res;
    }

    @Override
    public Boolean create(IncomeCreateFrom incomeCreateFrom) {
        Income income = new Income();

        BeanUtils.copyProperties(incomeCreateFrom,income);

        if(projectService.getById(income.getProjectId()) == null ||
            goodPeopleService.getById(income.getGoodPeopleId()) == null){

            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        income.setState(IncomeState.WAIT_ARGUE.getCode());

        if(income.getProtocolRootUrl() != null){
            income.setState(IncomeState.WAIT_GOODPEOPLE.getCode());
        }

        return this.save(income);
    }


    @Override
    public Boolean rewriteMoney(Integer id, BigDecimal money) {
        Income income = this.getById(id);

        if(income == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        income.setMoney(money);

        return this.updateById(income);
    }


    @Override
    public Boolean changeState(IncomeState oldState, IncomeState newState, Integer id, String fileUrl) {
        Income oldData = this.getById(id);

        if(oldData == null || !oldData.getState().equals(oldState.getCode())){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        oldData.setState(newState.getCode());

        if(!StringUtils.isEmpty(fileUrl)){
            switch (oldState){
                case WAIT_ARGUE:    //待协商
                    oldData.setProtocolRootUrl(fileUrl);    //协议原稿
                    break;
                case WAIT_GOODPEOPLE: //待捐赠方审核
                    oldData.setGoodPeopleFile(fileUrl); //捐赠方审核文件
                    break;
                case WAIT_JJ: //待基金会
                    oldData.setFoundationFile(fileUrl); //基金会审批文件
                    break;
                case WAIT_GET:  //待收款
                    oldData.setBankFile(fileUrl);   //银行收据文件
                    break;
                case ALREADY_GET:   //已收款
                    oldData.setRiteUrl(fileUrl);    //捐赠仪式照片
                    break;
                case OVER:
                    break;

            }
        }

        return this.updateById(oldData);
    }
}
